import { BadRequestException, Controller, UseGuards } from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { Get, Post, Param, Patch, Body, Delete } from '@nestjs/common'
import { ApiQuery } from '@nestjs/swagger'
import { CreateUserDto } from './dto/create-user.dto'
import { UpdateUserDto } from './dto/update-user.dto'
import { UserRole } from './roles.enum'
import { UserService } from './user.service'
import { RolesGuard } from 'src/auth/roles.guard'
import { Roles } from 'src/auth/roles.decorator'


@Controller('user')
@UseGuards(AuthGuard('jwt'))
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post()
  @Roles('admin')
  @UseGuards(RolesGuard)
  create(@Body() createUserDto: CreateUserDto) {
    return this.userService.create(createUserDto)
  }


  @Get()
  @Roles('admin')
  @UseGuards(RolesGuard)
  findAll() {
    return this.userService.findAll()
  }

  @Get('findByName/:username')
  @Roles('admin')
  @UseGuards(RolesGuard)
  async findOneByName(@Param('username') username: string) {
    const result = await this.userService.findOne(username)
    if (!result) {
      throw new BadRequestException("未找到数据")
    }
    return result
  }

  @Get(':id')
  @Roles('admin')
  @UseGuards(RolesGuard)
  async findOne(@Param('id') id: number) {
    const result = await this.userService.findOne(+id)
    if (!result) {
      throw new BadRequestException("未找到数据")
    }
    return result
  }

  @Patch(':id')
  @Roles('admin')
  @UseGuards(RolesGuard)
  update(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto)
  }

  @Delete(':id')
  @Roles('admin')
  @UseGuards(RolesGuard)
  remove(@Param('id') id: number) {
    return this.userService.remove(id)
  }
}
